package com.whz.mlc.configuration;

import com.whz.mlc.cache.stat.MlcStatManager;
import com.whz.mlc.factory.MlcCacheFactory;
import com.whz.mlc.provider.MlcCacheDefinition;
import com.whz.mlc.provider.loader.MlcCacheLoaderProvider;
import com.whz.mlc.provider.loader.MlcCompositeCacheLoader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;


@Configuration
public class MlcCacheConfiguration {

    /**
     * tmc second cache cacheLoader beans
     */
    @Bean
    @ConditionalOnMissingBean
    public MlcCacheLoaderProvider tmcDefaultTmcSecondCompositeCacheLoader(
            List<MlcCacheDefinition> mlcCacheDefinitions) {
        return new MlcCompositeCacheLoader(mlcCacheDefinitions);
    }

    @Bean
    @ConditionalOnMissingBean
    public MlcCacheFactory tmcCacheFactory(MlcCacheLoaderProvider mlcCacheLoaderProvider,
                                           MlcStatManager mlcStatManager) {
        MlcCacheFactory mlcCacheFactory = new MlcCacheFactory();
        // 提供全局缓存配置
        mlcCacheFactory.setMlcCacheLoaderProvider(mlcCacheLoaderProvider);
        // 缓存命中率统计
        mlcCacheFactory.setRedisMlcStatManager(mlcStatManager);
        return mlcCacheFactory;
    }
}